class Solution
{
public:
    int numSubarraysWithSum(vector<int> &nums, int goal)
    {
        partial_sum(nums.begin(), nums.end(), nums.begin());
        unordered_map<int, int> sumCount;
        sumCount[0] = 1;
        int n = nums.size();
        int result = 0;
        for (int i = 0; i < n; ++i)
        {
            result += sumCount[nums[i] - goal];
            ++sumCount[nums[i]];
        }
        return result;
    }
};